/*
 *    Copyright 2021 FightingGuys Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package cn.fightingguys.kaiheila;

import cn.fightingguys.kaiheila.config.SdkConfigurer;
import cn.fightingguys.kaiheila.config.api.ApiConfigurer;
import cn.fightingguys.kaiheila.config.core.Configurer;
import cn.fightingguys.kaiheila.config.event.EventSourceConfigurer;
import cn.fightingguys.kaiheila.config.event.WebSocketEventSourceConfigurer;
import cn.fightingguys.kaiheila.config.event.WebhookEventSourceConfigurer;

/**
 * 开黑啦 Sdk 实例建造类
 *
 * @author running_hyk
 * @version 1.0.0
 * @since 1.0.0
 */
public class KHLBuilder implements Configurer<KHL> {

    /**
     * Sdk 配置类
     */
    private final SdkConfigurer sdkConfigurer;

    /**
     * Api 配置类
     */
    private final ApiConfigurer apiConfigurer;

    /**
     * <p>事件源配置类</p>
     *
     * @see WebhookEventSourceConfigurer
     * @see WebSocketEventSourceConfigurer
     */
    private final EventSourceConfigurer eventSource;

    /**
     * 创建开黑啦 Sdk 实例建造者
     *
     * @return {@link KHLBuilder}
     * @see KHL
     * @see KHLBuilder
     * @since 1.0.0
     */
    public static KHLBuilder builder() {
        return new KHLBuilder();
    }

    /**
     * 构造函数，新建配置类对象
     */
    private KHLBuilder() {
        this.sdkConfigurer = new SdkConfigurer(this);
        this.apiConfigurer = new ApiConfigurer(this);
        this.eventSource = new EventSourceConfigurer(this);
    }

    /**
     * 使用默认配置构建 KHL SDK
     *
     * @param token 用户 API Token
     * @since 1.0.0
     */
    public KHL createDefault(String token) {
        this.apiConfigurer().token(token);
        return this.build();
    }

    /**
     * <p>开黑啦 Sdk 配置</p>
     *
     * @return {@link SdkConfigurer}
     * @see SdkConfigurer
     * @since 1.0.0
     */
    public SdkConfigurer sdkConfigurer() {
        return sdkConfigurer;
    }

    /**
     * <p>开黑啦 API 配置</p>
     *
     * @return {@link ApiConfigurer}
     * @see ApiConfigurer
     * @since 1.0.0
     */
    public ApiConfigurer apiConfigurer() {
        return apiConfigurer;
    }

    /**
     * <p>事件源配置</p>
     *
     * @return {@link EventSourceConfigurer}
     * @see EventSourceConfigurer
     * @since 1.0.0
     */
    public EventSourceConfigurer eventSource() {
        return eventSource;
    }

    /**
     * 创建当前 KHLBuilder KHL 实例
     *
     * @throws IllegalArgumentException API Token 为空
     * @return KHL
     */
    public KHL build() {
        if (apiConfigurer.getToken() == null) {
            throw new IllegalArgumentException("the api token cannot be null");
        }
        return new KhlKernel(sdkConfigurer, apiConfigurer, eventSource);
    }

}
